package com.test.demo.controller;

import com.test.demo.model.Account;
import com.test.demo.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @RequestMapping("/list")
    public List<Account> getAccountList(){
        return accountService.findAccountList();
    }

    @RequestMapping("/{id}")
    public Account getAccount(@PathVariable("id") int id){
        return accountService.findAccountById(id);
    }

    @RequestMapping("/update")
    public String updateAccount(@RequestParam("name") String name,@RequestParam("id") int id,@RequestParam("money") double money){
        Account account=new Account();
        account.setId(id);
        account.setName(name);
        account.setMoney(money);
        int result=accountService.update(account);
        if(result>0){
            return "success";
        }else{
            return "fail";
        }
    }
}
